﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace calculator
{
    public partial class Form1 : Form
    {
        double num1 = 0, num2 = 0; // 存储两个数字
        string op = ""; // 存储运算符
        bool isNewInput = true; // 判断是否为新输入

        public Form1()
        {
            InitializeComponent();
        }

        private void AppendNumber(string digit)
        {
            if (isNewInput) // 如果是新输入，则清空文本框
            {
                textBox1.Text = "";
                isNewInput = false;
            }

            textBox1.Text += digit; // 在文本框中添加数字
        }

        private void button0_Click(object sender, EventArgs e)
        {
            AppendNumber("0");
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            AppendNumber("1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AppendNumber("2");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            AppendNumber("3");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            AppendNumber("4");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            AppendNumber("5");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            AppendNumber("6");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            AppendNumber("7");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            AppendNumber("8");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            AppendNumber("9");
        }

        private void buttonAdd_Click_1(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text); // 将第一个数字存储起来
            op = "+"; // 存储运算符
            isNewInput = true; // 设置为新输入
        }

        private void buttonSubtract_Click_1(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            op = "-";
            isNewInput = true;
        }

        private void buttonMultiply_Click_1(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            op = "*";
            isNewInput = true;
        }

        private void buttonDevide_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            op = "/";
            isNewInput = true;
        }

        private void buttonEquals_Click_1(object sender, EventArgs e)
        {
            num2 = double.Parse(textBox1.Text); // 将第二个数字存储起来

            double result = 0;

            switch (op) // 根据运算符进行计算
            {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                case "*":
                    result = num1 * num2;
                    break;
                case "/":
                    result = num1 / num2;
                    break;
            }

            textBox1.Text = result.ToString(); // 在文本框中显示结果
            isNewInput = true; // 设置为新输入
        }

        private void buttonClear_Click_1(object sender, EventArgs e)
        {
            textBox1.Text = ""; // 清空文本框
            isNewInput = true; // 设置为新输入

        }
    }
}
